package com.wanmait.happyheating.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.pagehelper.PageInfo;
import com.wanmait.happyheating.pojo.MaterialsType;
import com.wanmait.happyheating.service.MaterialsTypeService;
import com.wanmait.happyheating.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
       耗材类型的Controller
       @author 明康 5月20日
*/

@RestController
@RequestMapping("/manage")
public class AdminMaterialsTypeController {

    @Resource
    private MaterialsTypeService materialsTypeService;

    /*
    查重
     */
    @RequestMapping("findMaterialsType")
    public Result findMaterials(@RequestBody MaterialsType materialsType){
        LambdaQueryWrapper<MaterialsType> materialsTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        materialsTypeLambdaQueryWrapper.eq(MaterialsType::getTitle,materialsType.getTitle());
        List<MaterialsType> materialsTypes = materialsTypeService.list(materialsTypeLambdaQueryWrapper);
        if(materialsTypes.size()>0){
            return Result.error("有重复");
        }
        {
            return Result.success("无重复");
        }
    }
    /*
    添加耗材分类
     */
    @RequestMapping("addMaterialsType")
    public Result addMaterialsType(@RequestBody MaterialsType materialsType){
        Boolean flag = materialsTypeService.save(materialsType);
        if(flag){
            return Result.success("添加成功");
        }
        else
        {
            return Result.error("添加失败");
        }
    }

    /*
    查询所有耗材分类
     */
    @RequestMapping("showMaterials")
    public Result showMaterialsType(@RequestBody MaterialsType materialsType , Integer pageNum,Integer pageSize){
        if(pageNum==null){
            pageNum=1;
        }
        if(materialsType.getId()==0&&!("".equals(materialsType.getTitle()))){
            PageInfo<MaterialsType> materialsTypes = materialsTypeService.likeMaterialsTypeByPage(pageNum,pageSize,materialsType.getTitle());
            return Result.success("查询成功",materialsTypes);
        }
            PageInfo<MaterialsType> materialsTypes = materialsTypeService.findMaterialsTypeByPage(pageNum,pageSize);
            return Result.success("查询成功",materialsTypes);


    }
    /*
    模糊查询
     */
    @RequestMapping("LikeMaterialsType")
    public Result LikeMaterialsType(@RequestBody MaterialsType materialsType){
        LambdaQueryWrapper<MaterialsType> materialsTypeLambdaQueryWrapperr = new LambdaQueryWrapper<>();
        materialsTypeLambdaQueryWrapperr.like(MaterialsType::getTitle,materialsType.getTitle());
        List<MaterialsType> materialsTypes = materialsTypeService.list(materialsTypeLambdaQueryWrapperr);
        return Result.success("查询成功！",materialsTypes);
    }

    /*
    修改耗材类型信息
     */
    @RequestMapping("updateMaterialsType")
    public Result updateMaterialsType(@RequestBody MaterialsType materialsType){
        if(materialsType.getId()!=null){
            Boolean flag = materialsTypeService.updateById(materialsType);
            if(flag){
                return Result.success("修改成功");
            }else {
                return Result.error("修改失败");
            }
        }
        else {
            return Result.error("无效的Id,修改失败");
        }
    }

    /*
    删除
     */
    @DeleteMapping("deleteMaterialsType{id:\\d+}")
    public Result deleteMaterialsType(@PathVariable Integer id){
        if(id!=null){
            Boolean flag = materialsTypeService.removeById(id);
            if(flag){
                return Result.success("删除成功");
            }else{
                return Result.error("删除失败");
            }
        }else{
            return Result.error("无效的Id,删除失败");
        }
    }

    /*
    批量删除
     */

    @DeleteMapping("deleteMaterialsTypes")
    public Result deleteMaterialsTypes(@RequestBody List<Integer> materialsTypeIds){
        if(materialsTypeIds!=null) {
            Boolean flag = materialsTypeService.removeByIds(materialsTypeIds);
            if(flag){
                return Result.success("删除成功");
            }else{
                return Result.error("删除失败");
            }
        }
        else{
            return Result.error("无效的数据，删除失败！");
        }
    }
}